﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntityObjects;
using DAL_V1.App_Code.DBBroker;
using BAL.Controller;
using System.Data;
using System.Data.SqlClient;

namespace PresentationLayer.View.DepartmentManager
{
    public partial class DelHeadV1 : System.Web.UI.Page
    {
            List<Employee> empe_list = new List<Employee>();
            Employee loginDepHead = new Employee();
            protected void Page_Load(object sender, EventArgs e)
            {
                // **** respective Department Employee will be displayed.*************
                    
                     loginDepHead = (Employee)Session["CurrentUser"];
                     empe_list = DelHeadAuthorityController.getDelHeadAuthorityController().selectEmployeeByDept(loginDepHead.DeptId.ToString());
                     GvEmpList.DataSource = empe_list;
                     GvEmpList.DataBind();
        

            }

            protected void GvEmpList_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                     GvEmpList.PageIndex = e.NewPageIndex;
                     GvEmpList.DataBind();

            }

        //Search by desired Employee name
            protected void btnSearch_Click(object sender, EventArgs e)
            {
                    Employee empe = new Employee();
                    empe = (Employee)Session["CurrentUser"];
                    //string Depname = empe.DeptId;
                    string Depname = loginDepHead.DeptId;
                    GvEmpList.DataSource = DelHeadAuthorityController.getDelHeadAuthorityController().getEmployeebyDeptIdAndEmpName(Depname, txtEmpName.Text.Trim());
                    GvEmpList.DataBind();
               
             }

        //Assign Department Head Authority to Selected Employee ( Roleid " 1" )
             protected void btnSubmit_Click(object sender, EventArgs e)
            {

                 string selEmp;
               
                 //find a row object from empe_List by selected employeeid
                if (ViewState["selEmp"] == null)
                {
                    lblmessage.Visible = true;
                    lblmessage.Text = "Please select employee to assign.";
                }
                else
                {
                    selEmp = Convert.ToString(ViewState["selEmp"]);
                    Employee myLocatedObj = empe_list.Find(delegate(Employee ep) { return ep.EmpId == selEmp; });
                    //update Department Authorized Head Role Id- 2 at Employee table
                    myLocatedObj.RoleId = 2;
                    int result = DelHeadAuthorityController.getDelHeadAuthorityController().updateEmployee(myLocatedObj, selEmp);


                    lblmessage.Text = "Successfully Delegated to Employee ID '" + ViewState["selEmp"] + "'.";
                    btnDelegate.Enabled = false;
                 }


            }

              protected void btnselect_Click(object sender, EventArgs e)
             {
                    LinkButton EmpIdButton = sender as LinkButton;
                    int employeeid = Convert.ToInt32(EmpIdButton.CommandArgument);
                    EmpIdButton.Enabled = false;

                    ViewState["selEmp"] = Convert.ToString(employeeid);
                    lblmessage.Visible = true;
                    lblmessage.Text = "Employee ID '" + ViewState["selEmp"] + "' has been selected.";
                   

             }

               protected void txtEmpName_TextChanged(object sender, EventArgs e)
              {
                  
               }

             

            
    
        
    }
}